<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('SAP入库单审核')"/>
    <style>
        [v-cloak] {
            display: none
        }
        .deer-key {
            width: 95px;
        }
        .el-table--scrollable-y .el-table__body-wrapper::-webkit-scrollbar {
            display: none;
        }
    </style>
</head>
<body>
<div class="container-div">
    <a class="btn btn-default" @click="closeModal()">
        <i class="fa fa-reply"></i> 返回
    </a>
    <el-row class="deer-panel">
        <span style="color: #409EFF">SAP入库单</span>
        <el-divider style="margin-top: 10px"></el-divider>
        <el-col :span="8">
            <div class="deer-ky">
                <div class="deer-key">外部交货单号：</div>
                <div class="deer-value" v-cloak>{{billInSapInfo.sapNo}}</div>
            </div>
        </el-col>
        <el-col :span="8">
            <div class="deer-ky">
                <div class="deer-key">供应商名称：</div>
                <div class="deer-value" v-cloak>{{billInSapInfo.supplierName}}</div>
            </div>
        </el-col>
        <el-col :span="8">
            <div class="deer-ky">
                <div class="deer-key">凭证日期：</div>
                <div class="deer-value" v-cloak>{{billInSapInfo.bldat}}</div>
            </div>
        </el-col>
        <el-col :span="8">
            <div class="deer-ky">
                <div class="deer-key">库存地点：</div>
                <div class="deer-value" v-cloak>原料库</div>
            </div>
        </el-col>
        <el-col :span="8">
            <div class="deer-ky">
                <div class="deer-key">过账日期：</div>
                <div class="deer-value" v-cloak>{{billInSapInfo.budat}}</div>
            </div>
        </el-col>
        <el-col :span="8">
            <div class="deer-ky">
                <div class="deer-key">过账状态：</div>
                <div class="deer-value" v-cloak>{{state[billInSapInfo.state]}}</div>
            </div>
        </el-col>
        <el-col :span="8">
            <div class="deer-ky">
                <div class="deer-key">入库类型：</div>
                <div class="deer-value" v-cloak>{{type[billInSapInfo.type]}}</div>
            </div>
        </el-col>
        <el-col :span="8">
            <div class="deer-ky">
                <div class="deer-key">单据状态：</div>
                <div class="deer-value" v-cloak>{{billState[billInSapInfo.billState]}}</div>
            </div>
        </el-col>
    </el-row>

    <el-row :gutter="20">
        <el-col :span="24">
            <el-tabs type="border-card" class="deer-panel" style="margin-top: 15px;padding-top: 0">
                <el-tab-pane>
                    <span slot="label"><i class="el-icon-date"></i> 单据明细</span>
                    <el-table :data="item.list" border stripe>
                        <el-table-column prop="matnr" label="物料编码" align="center"></el-table-column>
                        <el-table-column prop="itemName" label="物料描述" align="center" width="200"></el-table-column>
                        <el-table-column prop="sort" label="物料类型" align="center" width="120">
                            <template slot-scope="scope">
                                <span v-if="scope.row.sort ==1">
                                    钢材
                                </span>
                                <span v-if="scope.row.sort ==2">
                                    铜材
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="erfmg" label="数量" align="center" width="65"></el-table-column>
                        <el-table-column prop="erfmg_RECEIVED" label="已入数量" align="center" width="80"></el-table-column>
                        <el-table-column prop="erfme" label="单位" align="center" width="55"></el-table-column>
                        <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
                    </el-table>
                    <el-pagination
                            @current-change="chengCurrentPage"
                            :current-page="item.currentPage"
                            :page-size="item.pageSize"
                            layout="prev, pager, next"
                            :total="item.total">
                    </el-pagination>
                </el-tab-pane>
            </el-tabs>
        </el-col>
    </el-row>
    <el-row style="margin-top: 20px;" v-if="billInSapInfo.type == 4 || billInSapInfo.type == 5">
        <el-form ref="dynamicValidateForm" :inline="true" :model="billInSapInfo" class="demo-form-inline">
            <el-form-item style="margin-bottom: 5px" prop="sapNo" label="外部交货单号" :rules="{
              required: true, message: '外部交货单号不能为空', trigger: 'blur'
            }">
                <el-input size="small" v-model="billInSapInfo.sapNo" placeholder="外部交货单号"></el-input>
            </el-form-item>
        </el-form>
    </el-row>
    <el-row style="margin: 10px 0;">
        <el-button size="small" type="primary" @click="commitItems">审核通过</el-button>
        <el-button size="small" type="info" @click="closeModal">取消</el-button>
    </el-row>
    <div style="height: 20px;width: 100%"></div>
</div>

<div th:include="include::footer"></div>
<script th:inline="javascript">
    var prefix = ctx + "in/billInMaster";
    var detailPrefix = ctx + "in/billInSap";

    var billInSapId = parseInt(localStorage.getItem(detailPrefix+'/billInSapAudit'));

    var vue = new Vue({
        el: '.container-div',
        data: {
            billInSapInfo: {},
            item: {
                currentPage: 1,
                pageSize: 4,
                total: '',
                list: [],
            },
            type: {
                1: '采购入库',
                2: '生产退料',
                5: '采购预收',
            },
            state: {
                1: '未过账',
                2: '已过帐',
                3: '部分过账',
                4: '入库完成'
            },
            billState: {
                1: '待审核',
                2: '已审核',
                // 3: '处理中',
                // 4: '已完成'
            }
        },
        methods: {
            //关闭此页面
            closeModal(){
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            },
            commitItems() {
                var that = this;
                if (that.billInSapInfo.type == 3) {
                    this.$refs['dynamicValidateForm'].validate((valid) => {
                        if (valid) {
                            $.ajax({
                                cache: true,
                                type: "POST",
                                url: detailPrefix + '/auditManual',
                                dataType: "json",
                                contentType: "application/json;charset-UTF-8",
                                data: JSON.stringify({
                                    sapNo: that.billInSapInfo.sapNo,
                                    billInSapId: billInSapId
                                }),
                                async: false,
                                error: function(request){
                                    $.modal.alertError("系统错误");
                                },
                                success: function(data){
                                    if(data.code == 200){
                                        $.modal.msgReload(data.msg, "success")
                                    }else{
                                        $.modal.alertError(data.msg);
                                    }
                                }
                            });
                        } else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                } else {
                    $.ajax({
                        cache: true,
                        type: "POST",
                        url: detailPrefix + '/audit',
                        dataType: "json",
                        contentType: "application/json;charset-UTF-8",
                        data: JSON.stringify(that.billInSapInfo),
                        async: false,
                        error: function(request){
                            $.modal.alertError("系统错误");
                        },
                        success: function(data){
                            if(data.code == 200){
                                $.modal.msgReload(data.msg, "success")
                            }else{
                                $.modal.alertError(data.msg);
                            }
                        }
                    });
                }
            },
            //SAP收货单信息
            getBillInSapInfo(){
                var that = this;
                $.ajax({
                    cache: true,
                    type: "POST",
                    url: detailPrefix + '/findBillInSapByBillInSapId',
                    data: {
                        billInSapId: billInSapId
                    },
                    async: false,
                    error: function(request){
                        $.modal.alertError("系统错误");
                    },
                    success: function(data){
                        if(data.bldat != null){
                            data.bldat = data.bldat.slice(0, 19);
                        }
                        if(data.budat != null){
                            data.budat = data.budat.slice(0, 19);
                        }
                        that.billInSapInfo = data;
                    }
                });
            },
            //分页
            chengCurrentPage: function(currentPage){
                this.item.currentPage = currentPage;
                this.getDetails(null);
            },
            //SAP收货单明细
            getDetails(){
                var that = this;
                $.ajax({
                    url: detailPrefix + "/findBillInSapDetailByBillInSapId",
                    type: "POST",
                    data: {
                        billInSapId: billInSapId,
                        pageSize: that.item.pageSize,
                        pageNum: that.item.currentPage
                    },
                    error: function(request){
                        $.modal.alertError("系统错误");
                    },
                    success: function(data){
                        var msg = data.rows;
                        for(var i = 0; i < msg.length; i++){
                            msg[i].reality = parseFloat(msg[i].lgmng - msg[i].remain).toFixed(3);
                            if(msg[i].reality <= 0){
                                msg[i].reality = 0;
                            }else{
                                var zero = msg[i].reality.split('.')[1];
                                if(zero == '000'){
                                    msg[i].reality = msg[i].reality.split('.')[0];
                                }
                            }
                            if(msg[i].createTime != null){
                                msg[i].createTime = msg[i].createTime.slice(0, 19);
                            }
                        }
                        that.item.list = msg;
                        that.item.total = data.total;
                    }
                })
            },
            //导出
            detailExcel(){
                var url = "/in/billInSapDetail/export";
                var data = {"billInSapId": billInSapId};
                $.post(url, data, function(result){
                    if(result.code == web_status.SUCCESS){
                        window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
                    }else if(result.code == web_status.WARNING){
                        $.modal.alertWarning(result.msg);
                    }else{
                        $.modal.alertError(result.msg);
                    }
                    $.modal.closeLoading();
                });
            }
        },
        created(){
            this.getBillInSapInfo();
            this.getDetails();
        }
    })
</script>
</body>
</html>
